package com.ledim.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.ledim.adapter.base.LedimBaseListAdapter;
import com.ledim.bean.LedimChoiceCardBean;
import com.ledim.utils.DensityUtil;
import com.ledim.utils.FrescoUtil;
import com.letv.android.client.LetvSDK;
import com.letv.android.young.client.R;
import com.letv.core.bean.IVideo;

/**
 * 频道页adapter
 */
public class ChannelEpisodeAdapter extends LedimBaseListAdapter<LedimChoiceCardBean> {


    public ChannelEpisodeAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        //mHeight = mWidth*97/173;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_channel_episode_listview, null);
            holder.mImage = (SimpleDraweeView) convertView.findViewById(R.id.card_item_photo);
            holder.mName = (TextView) convertView.findViewById(R.id.item_album_name);
            holder.mEpisode = (TextView) convertView.findViewById(R.id.item_album_episode);
            holder.mView = convertView.findViewById(R.id.item_bottom_bg);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //获取屏幕的宽度
        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
        int widthPixels = dm.widthPixels;
        int photoHeight = (widthPixels / 2 - 15) * 9 / 16;

        ViewGroup.LayoutParams imageLp = holder.mImage.getLayoutParams();
        imageLp.height = photoHeight;

        ViewGroup.LayoutParams viewLp = holder.mView.getLayoutParams();
        viewLp.height = photoHeight;

        holder.mName.setPadding(0, 0, 5, 0);
        if (position % 2 == 0) {
            convertView.setPadding(DensityUtil.dip2px(context, 10), DensityUtil.dip2px(context, 10), DensityUtil
                    .dip2px(context, 5), 0);
        } else {
            convertView.setPadding(DensityUtil.dip2px(context, 5), DensityUtil.dip2px(context, 10), DensityUtil
                    .dip2px(context, 10), 0);
        }
        final LedimChoiceCardBean card = getItem(position);
        String picUrlString = (card.type == 2) ? card.album.images.pic169 : card.photo.thumb;
        if (picUrlString.length() > 0 && card.photo != null) {
            FrescoUtil.loadUrl(picUrlString, holder.mImage);
        }
        holder.mName.setText(card.title);
        if (card.album != null) {
            if (card.subtitle.equals("电视剧") || card.subtitle.equals("动漫")) {
                holder.mEpisode.setVisibility(View.VISIBLE);
                if (card.album.isEnd == 1) {
                    holder.mEpisode.setText(card.album.episodes + "集全");
                } else {
                    if (card.album.nowEpisodes == 0) {
                        holder.mEpisode.setVisibility(View.GONE);
                    } else {
                        holder.mEpisode.setText("更新至第" + card.album.nowEpisodes + "集");
                    }

                }
            } else if (card.subtitle.equals("综艺")) {
                holder.mEpisode.setVisibility(View.VISIBLE);
                if (card.album.isEnd == 1) {
                    if (card.album.episodes == 0) {
                        holder.mEpisode.setVisibility(View.GONE);
                    } else {
                        holder.mEpisode.setText(getEpisodes(card.album.episodes + "") + "期全");
                    }
                } else {
                    if (card.album.nowEpisodes == 0) {
                        holder.mEpisode.setVisibility(View.GONE);
                    } else {
                        holder.mEpisode.setText(
                                "更新至" + getEpisodes(card.album.nowEpisodes + "") + "期");
                    }
                }
            }
        } else {
            holder.mEpisode.setVisibility(View.GONE);
        }

        holder.mImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IVideo video1 = new IVideo();
                video1.setmVid(0);
                LetvSDK.getInstance().play(context, video1, false, card);
            }
        });

        return convertView;
    }

    public static class ViewHolder {

        private SimpleDraweeView mImage;
        private TextView mName;
        private TextView mEpisode;
        private View mView;

    }

    private String getEpisodes(String str) {
        String strEnd;
        if (str.length() == 8) {
            strEnd = str.substring(0, 4) + "-" + str.substring(4, 6) + "-" +
                    str.substring(6, str.length());
        } else {
            strEnd = str;
        }
        return strEnd;
    }

}
